package com.jjwxc.jwjskandriod.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.activity.BookDetailsActivity;
import com.jjwxc.jwjskandriod.adapter.DerivativeViewHolder;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFActivity;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter;
import com.jjwxc.jwjskandriod.model.ListWeekBookResopnse;
import com.jjwxc.jwjskandriod.readActivity.utils.UMConstant;
import com.jjwxc.jwjskandriod.widget.LinearLayouDecoration;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ListRankFragment extends Fragment {
    private View view;

    private RecyclerView rv_derivativeinfo;

    private ListInfoAdapter adapter;
    String key;
    String value;

    public ListRankFragment(String key, String value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_derivativeinfo, container, false);
            rv_derivativeinfo = view.findViewById(R.id.rv_derivativeinfo);
            setView();
        }
        listDateRankBook();
        return view;
    }

    public void setView() {
        //设置不可滑动
        rv_derivativeinfo.setLayoutManager(new GridLayoutManager(getActivity(), 2) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        rv_derivativeinfo.addItemDecoration(new LinearLayouDecoration(getActivity(), LinearLayouDecoration.HORIZONTAL, R.dimen.border, R.color.transparent));
        rv_derivativeinfo.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        adapter = new ListInfoAdapter(getActivity());
        rv_derivativeinfo.setAdapter(adapter);

    }

    public void listDateRankBook() {
        ((FFActivity) getActivity()).post(Url.listDateRankBook, null, ListWeekBookResopnse.class, new FFNetWorkCallBack<ListWeekBookResopnse>() {
            @Override
            public void onSuccess(ListWeekBookResopnse response) {
                if (response.getCode() == 200) {
                    adapter.setData(response.getData());
                }

            }

            @Override
            public boolean onFail(ListWeekBookResopnse response) {
                return false;
            }
        }, "{\"limit\":10,\"tab\":\"" + key + "\",\"rankType\":\"" + value + "\"}");
    }

    public class ListInfoAdapter extends BaseRecyclerAdapter<DerivativeViewHolder> {
        private Context context;


        private List<ListWeekBookResopnse.DataBean> mData = new ArrayList<>();


        public ListInfoAdapter(Context cnt) {
            this.context = cnt;

        }

        public void addData(List<ListWeekBookResopnse.DataBean> data) {
            if (data != null) {
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }

        public void setData(List<ListWeekBookResopnse.DataBean> data) {
            this.mData.clear();
            addData(data);
        }

        /**
         * 创建ViewHolder
         */
        @Override
        public DerivativeViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {

            View mView = LayoutInflater.from(context).inflate(R.layout.derivative_item_grid, parent, false);
            DerivativeViewHolder viewHolder_text = new DerivativeViewHolder(mView);
            viewHolder_text.setIsRecyclable(false);
            return viewHolder_text;

        }

        @Override
        public DerivativeViewHolder getViewHolder(View view) {
            return new DerivativeViewHolder(view);
        }


        /**
         * 绑定数据：可以直接拿到已经绑定控件的Viewholder对象
         */
        @SuppressLint({"SetTextI18n", "ClickableViewAccessibility", "ResourceAsColor"})
        @Override
        public void onBindViewHolder(final DerivativeViewHolder holder, @SuppressLint("RecyclerView") final int position, boolean isItem) {
            holder.tv_title_grid.setText(mData.get(position).getName());
            holder.tv_author.setText(mData.get(position).getBookShortIntro());
            holder.tv_num.setText(position + 1 + "");

            if (position < 3) {
                holder.tv_num.setTextColor(getResources().getColor(R.color.white));
                holder.tv_num.setBackgroundResource(R.mipmap.num_bg);
            } else {
                holder.tv_num.setBackground(null);
                holder.tv_num.setTextColor(getResources().getColor(R.color.texthui));
            }
            holder.cl_derivate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), BookDetailsActivity.class);
                    intent.putExtra("bookId", mData.get(position).getBookId());
                    startActivity(intent);
                    switch (key) {
                        case "1":
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANQING_BANGDAN3_CLICK);
                            break;
                        case "2":
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_CHUNAI_BANGDAN3_CLICK);
                            break;
                        case "3":
                            MobclickAgent.onEvent(getContext(), UMConstant.SUGGEST_YANSHENG_BANGDAN3_CLICK);
                            break;
                    }
                }
            });


        }

        @Override
        public int getAdapterItemViewType(int position) {
            return position;
        }


        @Override
        public int getAdapterItemCount() {
            return mData.size();
        }


    }
}